var tree_store;
var menu_tree;
var tabs;
Ext
		.onReady(function() {

			var tree_url = rootPath + "/menu/getMenuTree.do";
			tree_store = Ext.create('Ext.data.TreeStore', {
				storeId : 'tree_store_',
				fields : [ 'id', 'text', 'pid', 'level', 'url', 'seq' ],
				proxy : {
					type : 'ajax',
					url : tree_url,
					reader : {
						type : 'json',
						root : 'tree_items'
					}
				}
			});
			menu_tree = Ext.create('Ext.tree.Panel', {
				id : 'menu_tree_',
				title : '菜单',
				selModel : true,
				rootVisible : true,
				store : tree_store,
				autoHeight : true,
				minHeight : 400,
				border : false,
				margin : '2 0 0 0',
				root : {
					expanded : true,
					id : "1",
					text : '菜单',
					level : 1
				},
				draggable : true
			});
			menu_tree.on("itemclick", treeClick);

			var iframe_html = '<iframe scrolling="auto" id="iframe_center" frameborder="0" width="100%" height="100%" src="" ></iframe>';

			tabs = Ext.create('Ext.tab.Panel', {
				id : 'center-Panel',
				activeTab : 0,
				boder : true,
				items : [ {
					title : '开始',
					layout : 'fit',
					html : "<div >寂寞啊如血啊</div>"
				} ]
			});

			Ext
					.create(
							'Ext.container.Viewport',
							{
								layout : 'border',
								renderTo : Ext.getBody(),
								items : [
										{
											region : 'north',
											html : '<div style="font-size:22px" >SPRING3MVC TEST</div>',
											height : 50,
											border : false,
											margins : '0 0 5 0'
										}, {
											region : 'west',
											title : '菜单',
											collapsible : true,
											width : "20%",
											layout : 'fit',
											split : true,
											items : [ menu_tree ]
										}, {
											region : 'center',
											layout : 'fit',
											items : tabs
										} ]
							});
		});
function treeClick(view, record, item, index, e) {
	if (!record.get("leaf") || record.data.url == null || record.data.url == "")
		return;
	var id = record.data.id;
	var tab = Ext.getCmp("tab_" + id);
	if (!tab) {
		var iframeHtm = '<iframe scrolling="auto" id="iframe_' + id + '" ';
		iframeHtm += ' frameborder="0" width="100%" height="100%" src="'
		iframeHtm += rootPath + encodeURI(record.data.url);
		iframeHtm += '" ></iframe>';
		tabs.add({
			id : "tab_" + id,
			title : record.data.text,
			closable : true,
			closeAction:'destroy',
			forceFit : true,
			html : iframeHtm,
			listeners:{
				close:function(){
					var ifm = document.getElementById("iframe_" + id);
					ifm.contentWindow.document.write('');//清空iframe的内容
					ifm.contentWindow.close();//避免iframe内存泄漏
					ifm.remove();//删除iframe
					if (CollectGarbage) {
	                    CollectGarbage(); //IE 特有 释放内存
	                }
				}
			}
		});
	} else {
		document.getElementById("iframe_" + id).src = rootPath
				+ record.data.url;
	}
	tabs.setActiveTab("tab_" + id);
}
